package android.hermes;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * @author Erik Tevell, Joakim Nels�n & Tobias Johansson
 *
 */
public class HermesMessage {
	    
    private int destination, author, hopCount;
	private String message = "", fileName;
	private long created;
	private byte[] data;
	
	
	/**
	 * @param indata
	 */
	public HermesMessage(DataInputStream indata){
		String inString;
		byte buf[], args[] = null;
		int j = 0;
		
		try {
			buf = new byte[indata.available()];
			indata.read(buf);
			
			//Separates args from data
			for(int i=0; i<buf.length; i++){
				if (j == 0 && buf[i]== 0){
					j = 1;
					args = new byte[i];
					System.arraycopy(buf, 0, args, 0, i);
				}
				else if(j == 1){
					j = 2;
					data = new byte[buf.length-(i)];
					System.arraycopy(buf, i, data, 0, buf.length-i);
					break;
				}
			}
			inString = new String(args);
			Scanner sc = new Scanner(inString);
			
			//Reads arguments; 0=destination(ID), 1=author(ID), 2=hopCount(int), 3=created left(int), 4=filename(string), 5=message(string)
			for(int i=0; sc.hasNextLine(); i++){
				if(i==0){
					destination = Integer.parseInt(sc.nextLine());
				}
				else if(i==1){
					author = Integer.parseInt(sc.nextLine());
				}
				else if(i==2){
					hopCount = Integer.parseInt(sc.nextLine());
				}
				else if(i==3){
					created = Long.parseLong(sc.nextLine());
				}
				else if(i==4){
					fileName = sc.nextLine();
				}
				else{
					message += sc.nextLine();
				}
			}
			
		} catch (IOException e) {
			// Error handling
			
		}
		
	}
	
	/**
	 * @param destination
	 * @param author
	 * @param hopCount
	 * @param created
	 * @param fileName
	 * @param message
	 * 
	 */
	public HermesMessage(int destination, int author, int hopCount, long created, String fileName, String message){
		byte buf[] = null;
		this.destination = destination;
		this.author = author;
		this.hopCount = hopCount;
		this.created = created;
		this.fileName = fileName;
		this.message = message;
		File fileSource = new File("/data/data/android.hermes/files/"+fileName);
		File fileSend = new File("/data/data/android.hermes/send/"+fileName);

		buf = new byte[(int)fileSource.length()];
		try{
			 FileInputStream FIS = new FileInputStream(fileSource);
			 FIS.read(buf);
			 FileOutputStream FOS = new FileOutputStream(fileSend);
			 FOS.write(buf);
		}catch(Exception e){
			 
		}
		 
	}
	
	public byte[] encodeMsg(){
		String tempArgs;
		byte buf[]=null, args[];
		try{
			//Writes arguments; 1=receiver(ID), 2=sender(ID), 3=Hops left(int), 4=time left(long), 5=filename(string), 6=message(string)
			tempArgs = destination + "\n"
					+ author + "\n"
					+ hopCount + "\n"
					+ created + "\n"
					+ fileName + "\n"
					+ message;
			
				
				
				//adds null to separate args from data
				args = (tempArgs+"\u0000").getBytes();
				
				if(fileName.length() > 0){
					
					File file = new File("/data/data/android.hermes/send/"+fileName);
					data = new byte[(int)file.length()];
					
					FileInputStream FIS = new FileInputStream(file);
					FIS.read(data);
				
					buf = new byte[args.length + (int)file.length()];
					System.arraycopy(args, 0, buf, 0, args.length);
					System.arraycopy(data, 0, buf, args.length, data.length);
					
					
				}
				else {
					//System.arraycopy(args, 0, buf, 0, args.length);
					buf = args;
				}
					
			} catch (IOException e) {
				// Error handling
				
			}
		return buf;
	}
	//decrease noOf hops left 
	public void decHopCount(){
		hopCount -= 1;
	}
	
	//Get methods
	public int getDestination(){
		return destination;
	}
	public int getAuthor(){
		return author;
	}
	public int getHopCount(){
		return hopCount;
	}
	public long getCreated(){
		return created;
	}
	public String getFileName(){
		return fileName;
	}
	public String getMsg(){
		return message;
	}
	public byte[] getData(){
		return data;
	}
	public void flushData(){
		data = null;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + author;
		result = prime * result + (int) (created ^ (created >>> 32));
		result = prime * result + destination;
		result = prime * result
				+ ((fileName == null) ? 0 : fileName.hashCode());
		result = prime * result + ((message == null) ? 0 : message.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		HermesMessage other = (HermesMessage) obj;
		if (author != other.author)
			return false;
		if (created != other.created)
			return false;
		if (destination != other.destination)
			return false;
		if (fileName == null) {
			if (other.fileName != null)
				return false;
		} else if (!fileName.equals(other.fileName))
			return false;
		if (message == null) {
			if (other.message != null)
				return false;
		} else if (!message.equals(other.message))
			return false;
		return true;
	}
}
